function checkForDublicateUsername(username) {
 if (username.length == 0) {
  return
 }

 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "includes/checkForDublicateUsername.php?username=" + username + "&sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   if (xmlHttp.responseText == "true") {
    document.getElementById("username").value = "Username in use!"
    document.getElementById("infoBox").innerHTML = "<p>Username already in use!</p>"
   }
  }
 }
}


function swichStartingLanguage(currentLanguage) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "includes/settingsSwich.php?currentLanguage=" + currentLanguage + "&sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("startingLanguage").innerHTML = xmlHttp.responseText
   document.getElementById("infoBox").innerHTML = "<p>Starting language is set to <b>" + xmlHttp.responseText + "</b></p>"
  }
 }
}


function showSwichDictionaries() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "dictionarySwichForm.php"
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
  }
 }
}


function setDictionary(dictionary, isChecked) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 if (isChecked) {
  var url = "dictionarySwichPost.php?f=add&dictionary=" + dictionary
 }

 else {
  var url = "dictionarySwichPost.php?f=remove&dictionary=" + dictionary
 }

 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("infoBox").innerHTML = xmlHttp.responseText
  }
 }
}


function showUsers() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "usersMenu.php?sid" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
  }
 }
}


function showUser(user) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "usersInfo.php?user=" + user + "&sid" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("infoBox").innerHTML = xmlHttp.responseText
  }
 }
}


function showQuery() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "queryForm.php?sid" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
   document.getElementById("theForeign").focus()
  }
 }
}


function checkWordAppearance(startingLanguage, minimumPoints, numberOfWordPair, theForeign) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "queryPost.php?startingLanguage=" + startingLanguage + "&minimumPoints=" + minimumPoints + "&numberOfWordPair=" + numberOfWordPair + "&theForeign=" + theForeign + "&sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   if (xmlHttp.responseText == "true") {
    document.getElementById("infoBox").innerHTML = "<p>Good!</p>"
   }

   else {
    document.getElementById("infoBox").innerHTML = xmlHttp.responseText
   }

   showQuery()

  }
 }
}


function showAddWordsMenu() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "addWordsMenu.php?sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
  }
 }
}


function showAddWordsForm(varsource, dictionary) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 if (varsource == "detailed") {
  var language1 = document.getElementById("language").value
  var bookSerie = document.getElementById("bookSerie").value
  var bookNumber = document.getElementById("bookNumber").value
  var chapter = document.getElementById("chapter").value

  var url = "addWordsForm.php?detailed=true&language=" + language1 + "&bookSerie=" + bookSerie + "&bookNumber=" + bookNumber + "&chapter=" + chapter + "&sid=" + Math.random()
 }

 else if (varsource == "freeNamed") {
  var dictionary = document.getElementById("dictionary").value

  var url = "addWordsForm.php?freeNamed=true&dictionary=" + dictionary + "&sid=" + Math.random()
 }

 else if (varsource == "exists") {
  var url = "addWordsForm.php?freeNamed=true&dictionary=" + dictionary + "&sid=" + Math.random()
 }

 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
   document.getElementById("foreign").focus()
  }
 }
}


function addWordsPost() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var dictionary = document.getElementById("dictionary").value
 var foreign = document.getElementById("foreign").value
 var local = document.getElementById("local").value

 var url = "addWordsPost.php?dictionary=" + dictionary + "&local=" + local + "&foreign=" + foreign + "&sid=" + Math.random()

 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   if (xmlHttp.responseText == "true") {
    document.getElementById("infoBox").innerHTML = "<p>Word <b>" + local + " - " + foreign + "</b> added to <b>" + dictionary + "</b></p>"

    document.getElementById("foreign").value = ""
    document.getElementById("local").value = ""
    document.getElementById("foreign").focus()
   }

   else {
    document.getElementById("infoBox").innerHTML = "<p>There were problems in the operation:" + xmlHttp.responseText + "</p>"
   }
  }
 }
}


function addUser() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var username = document.getElementById("username").value
 var password = document.getElementById("password").value

 var url = "addUser.php?username=" + username + "&password=" + password + "&sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   showIndex()

   if (xmlHttp.responseText == "true") {
    document.getElementById("infoBox").innerHTML = "<p>A new user added!</p>"
   }

   else if (xmlHttp.responseText == "invalid username") {
    document.getElementById("infoBox").innerHTML = "<p>Invalid username: Use only alpabetic characters!</p>"
   }

   else {
    document.getElementById("infoBox").innerHTML = "<p>There were problems in the operation:" + xmlHttp.responseText + "</p>"
   }
  }
 }
}


function showAdmin() {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 var url = "admin.php?sid=" + Math.random()
 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("bodybox").innerHTML = xmlHttp.responseText
  }
 }
}


function admin(f, object) {
 xmlHttp = GetXmlHttpObject()

 if (xmlHttp == null) {
  alert ("Browser does not support HTTP Request")
  return
 }

 if (f == "removeUser") {
  var url = "admin.php?f=removeUser&user=" + object + "&sid=" + Math.random()
 }

 if (f == "userToAdmin") {
  var url = "admin.php?f=userToAdmin&user=" + object + "&sid=" + Math.random()
 }

 if (f == "removeDictionary") {
  var url = "admin.php?f=removeDictionary&dictionary=" + object + "&sid=" + Math.random()
 }

 xmlHttp.onreadystatechange = stateChanged
 xmlHttp.open("GET", url, true)
 xmlHttp.send(null)

 function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
   document.getElementById("infoBox").innerHTML = xmlHttp.responseText
   showAdmin()
  }
 }
}



function GetXmlHttpObject() {
 var objXMLHttp = null

 if (window.XMLHttpRequest) {
  objXMLHttp = new XMLHttpRequest()
 }

 else if (window.ActiveXObject) {
  objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
 }

 return objXMLHttp
}